<?php

/**
 * 论坛基础控制器
 */

namespace app\topics\controller;

class TopicsController extends \app\base\controller\SiteController {

    protected $topicsConfig = [];
    protected $infoModule = [];
    protected $userInfo = [];
    protected $action = '';
    protected $noLogin = false;
    protected $memberInfo = [];

    public function __construct() {
        parent::__construct();
        $this->topicsConfig = target('topics/TopicsConfig')->getConfig();
        $this->initLogin();
        $this->action = request('get', 'action');
        if ($this->action) {
            $this->assign('action', $this->action);
        }
    }

    /**
     * 初始化登录状态
     */
    protected function initLogin() {
        $userInfo = $this->getLogin();
        if (!$this->noLogin && !$userInfo) {
            if (!isAjax()) {
                $this->redirect(url('topics/Login/index', ['action' => URL]));
            } else {
                $this->error('您尚未登录,请先登录进行操作!', url('topics/Login/index'), 401);
            }
        }
        $this->userInfo = $userInfo;
        define('USER_ID', $userInfo['user_id']);
        $this->assign('userInfo', $this->userInfo);
    }

    /**
     * 获取登录信息
     * @return array|bool
     */
    protected function getLogin() {
        $login = \dux\Dux::cookie()->get('topics_login');
        $userInfo = target('topics/TopicsUser')->getWhereInfo([
            'user_id' => $login['uid']
        ]);
        if (empty($userInfo)) {
            return false;
        }
        $config = \dux\Config::get('dux.use');
        $verify = sha1($userInfo['password'] . $config['safe_key']);
        if ($login['token'] <> $verify) {
            return false;
        }

        return $userInfo;
    }

    /**
     * 检测登录信息
     * @param string $uid
     * @param string $token
     * @return array|bool
     */
    public function checkLogin($uid = '', $token = '') {
        if (empty($uid) || empty($token)) {
            return false;
        }
        $userInfo = [];
        $info = target('topics/TopicsUser')->getWhereInfo([
            'user_id' => $uid
        ]);
        $userInfo['name'] = $info['username'];
        if (empty($info)) {
            return false;
        }
        $config = \dux\Config::get('dux.use');
        $verify = sha1($info['password'] . $config['safe_key']);
        if ($token <> $verify) {
            return false;
        }
        return $userInfo;
    }

    /**
     * 系统模板输出
     * @param string $tpl
     * @param bool $autoDir
     */
    protected function topicsDisplay($tpl = '', $autoDir = true) {
        $this->assign('sysPublic', $this->publicUrl);
        $this->assign('site', $this->siteConfig);
        $this->assign('topicsConfig', $this->topicsConfig);
        $this->assign('topicsType', target('topics/Topics', 'service')->getTypeAll());


        $data = array();
        $list = hook('service', 'menu', 'topics');
        foreach ((array)$list as $value) {
            $data = array_merge_recursive((array)$data, (array)$value);
        }
        $data = array_sort($data, 'order');
        $this->assign('topicsMenu', $data);

        $this->layout = 'app/topics/view/theme/common/common';
        if (!empty($tpl) && $autoDir) {
            $tpl = 'app/' . APP_NAME . '/view/theme/' . strtolower(MODULE_NAME) . '/' . strtolower($tpl);
        }
        if (empty($tpl)) {
            $tpl = 'app/' . APP_NAME . '/view/theme/' . strtolower(MODULE_NAME) . '/' . strtolower(ACTION_NAME);
        }
        $this->display($tpl);
        exit;
    }

    /**
     * 分页处理
     */
    protected function _pageHtml($pageData, $params) {
        $html = '<ul class="uk-pagination  uk-flex-center uk-margin"><li class="uk-disabled"><a href="' . $pageData['prevUrl'] . '">&laquo;</a></li>';
        foreach ($pageData['pageList'] as $vo) {
            if ($vo == $pageData['current']) {
                $html .= '<li  class="uk-active"><a href="javascript:;">' . $vo . '</a></li>';
            } else {
                $html .= '<li><a href="' . $this->createPageUrl($vo, $params) . '">' . $vo . '</a></li>';
            }
        }
        $html .= '<li><a href="' . $pageData['nextUrl'] . '">&raquo;</a></li></ul>';
        return $html;
    }

    protected function page404() {
        \dux\Dux::header(404, function () {
            if (!headers_sent()) {
                header("Content-Type: text/html; charset=UTF-8");
            }
            $this->topicsDisplay('app/topics/view/' . LAYER_NAME . '/common/404', false);
        });
    }

    public function getImgCode() {
        return new \dux\lib\Vcode(90, 37, 4, '', 'code');
    }

}